package cn.changein.framework.service.setting.contactType.impl;

import cn.changein.framework.base.pojo.page.PageFactory;
import cn.changein.framework.kernel.model.response.ResponseData;
import cn.changein.framework.model.setting.client.mapper.ClientMapper;
import cn.changein.framework.model.setting.contactType.entity.ContactType;
import cn.changein.framework.model.setting.contactType.mapper.ContactTypeMapper;
import cn.changein.framework.model.setting.contactType.model.params.ContactTypeParam;
import cn.changein.framework.model.setting.contactType.model.result.ContactTypeResult;
import cn.changein.framework.service.setting.contactType.ContactTypeService;
import cn.changein.framework.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 单位类型 服务实现类
 * </p>
 *

 * @Date 2020-10-27
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
@Service
public class ContactTypeServiceImpl extends ServiceImpl<ContactTypeMapper, ContactType> implements ContactTypeService {

    private static final Integer DELETE_FLAG = 0;

    @Autowired
    private ClientMapper clientMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData add(ContactTypeParam param) {
        if (StringUtils.isEmpty(param.getName())) {
            return ResponseData.error("内容不能为空");
        }
        // 校验当前新增的单位类型名称是否已经存在
        Integer nameCount = this.baseMapper.countByName(param.getName(), null, DELETE_FLAG);
        if (nameCount != null && nameCount > 0) {
            return ResponseData.error(param.getName() + "已存在");
        }
        ContactType entity = getEntity(param);
        this.save(entity);
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData update(ContactTypeParam param) {
        if (StringUtils.isEmpty(param.getName())) {
            return ResponseData.error("内容不能为空");
        }
        // 校验当前修改的单位类型名称是否已经存在
        Integer nameCount = this.baseMapper.countByName(param.getName(), param.getId(), DELETE_FLAG);
        if (nameCount != null && nameCount > 0) {
            return ResponseData.error(param.getName() + "已存在");
        }
        ContactType oldEntity = getOldEntity(param);
        ContactType newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData delete(ContactTypeParam param) {
        if (param.getId() == null) {
            return ResponseData.error("删除异常");
        }
        // 根据ID查询单位类型信息
        ContactType contactType = this.baseMapper.selectById(param.getId());
        if (contactType == null) {
            return ResponseData.error("单位类型不存在");
        }
        // 校验数据是否存在或者状态为已删除
        if (contactType.getDeleteFlag().intValue() == 1) {
            return ResponseData.error(contactType.getName() + "已被删除");
        }
        // 验证所删除的单位类型是否被客户所引用
        Integer clientContactId = clientMapper.countByClientContactId(param.getId());
        if (clientContactId != null && clientContactId > 0) {
            return ResponseData.error(contactType.getName() + "已被客户数据所引用");
        }
        contactType.setDeleteFlag(1);
        this.baseMapper.updateById(contactType);
        return ResponseData.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData deleteList(String[] contactTypeIds) {
        if (contactTypeIds == null || contactTypeIds.length == 0) {
            return ResponseData.error("请选择至少一条数据");
        }
        // 验证该删除的单位类型是否被客户所引用
        List<Integer> deleteIdsByClient = this.baseMapper.listDeleteIdsByClient(contactTypeIds);
        if (deleteIdsByClient == null || deleteIdsByClient.isEmpty()) {
            return ResponseData.error("所选择的单位类型已被客户数据所引用");
        }
        // 校验所删除的单位类型是否被供应商所引用
        List<Integer> deleteIdsBySupplier = this.baseMapper.listDeleteIdsBySupplier(deleteIdsByClient);
        if (deleteIdsBySupplier == null || deleteIdsBySupplier.isEmpty()) {
            return ResponseData.error("所选择的单位类型已被供应商数据所引用");
        }
        this.baseMapper.deleteList(deleteIdsBySupplier);
        return ResponseData.success();
    }

    @Override
    public ContactTypeResult findBySpec(ContactTypeParam param) {
        return this.baseMapper.findBySpec(param, DELETE_FLAG);
    }

    @Override
    public List<ContactTypeResult> findListBySpec(ContactTypeParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public Page<Map<String, Object>> findPageBySpec(ContactTypeParam param) {
        if (!StringUtils.isEmpty(param.getSearchDate())) {
            param.setSearchDate(param.getSearchDate().substring(0, 10));
        }
        return this.baseMapper.customPageMapList(PageFactory.defaultPage(), param, DELETE_FLAG);
    }

    private Serializable getKey(ContactTypeParam param) {
        return param.getId();
    }

    private ContactType getOldEntity(ContactTypeParam param) {
        return this.getById(getKey(param));
    }

    private ContactType getEntity(ContactTypeParam param) {
        ContactType entity = new ContactType();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
